################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Analysis ESS
#PTable C 4
#
#Verena Fetscher
#July 2022
####################

####################
# Load packages
####################

library(tidyverse)
library(xtable)
library(Amelia)

####################
# Load data
####################
source("dataframes.R")



##########################
#Summary statistics
##########################
summean<-NULL
for(i in 1:5) {
  sumry <- mi.out[[i]] %>%
    summarise(red=mean(redistribution),
              inc=mean(income.PPP.th),
              incdist=mean(income.dist.th),
              our=mean(our),
              age=mean(agea),
              education=mean(eduyrs),
              benefit=mean(benefitsAvg),
              gini=mean(gini.disp),
              immigration=mean(immigration),
              social_spending=mean(social_spending),
              toptax=mean(toptax),
              red_transfers=mean(red_transfers,na.rm=T),
              single=mean(single)*100,
              gender=mean(gender)*100,
              unemployment=mean(uempl)*100)
  summean <- rbind(summean, sumry[,1:15])
}

sumsd<-NULL
for(i in 1:5) {
  sumry <- mi.out[[i]] %>%
    summarise(redsd=sd(redistribution),
              incsd=sd(income.PPP.th),
              incdistsd=sd(income.dist.th),
              oursd=sd(our),
              agesd=sd(agea),
              educationsd=sd(eduyrs),
              benefitsd=sd(benefitsAvg),
              ginisd=sd(gini.disp),
              immigrationsd=sd(immigration),
              social_spending=sd(social_spending),
              toptax=sd(toptax),
              red_transfers=sd(red_transfers,na.rm=T),
              singlesd=0,
              gendersd=0,
              unemploymentsd=0)
  sumsd <- rbind(sumsd, sumry[,1:15])
}

summean[3]
sumsd[3]
allmean<-NULL
allsd<-NULL
for(i in 1:15) {
  combined.results <- mi.meld(q = summean[i], se = sumsd[i])
  allmean <- rbind(allmean,combined.results$q.mi)
  allsd <- rbind(allsd,combined.results$se.mi)
}

df_imp<-data.frame(cbind(round(allmean,2),round(allsd,2)))

data_list %>%
  summarise(red=mean(redistribution),
            inc=mean(income.PPP.th),
            incdist=mean(income.dist.th),
            our=mean(our),
            age=mean(agea),
            education=mean(eduyrs),
            benefit=mean(benefitsAvg),
            gini=mean(gini.disp),
            immigration=mean(immigration),
            social_spending=mean(social_spending),
            toptax=mean(toptax),
            red_transfers=mean(red_transfers,na.rm=T),
            single=mean(single)*100,
            gender=mean(gender)*100,
            unemployment=mean(uempl)*100) -> summean_list

data_list %>%
  summarise(red=sd(redistribution),
            inc=sd(income.PPP.th),
            incdist=sd(income.dist.th),
            our=sd(our),
            age=sd(agea),
            education=sd(eduyrs),
            benefit=sd(benefitsAvg),
            gini=sd(gini.disp),
            immigration=sd(immigration),
            social_spending=sd(social_spending),
            toptax=sd(toptax),
            red_transfers=sd(red_transfers,na.rm=T),
            single=0,
            gender=0,
            unemployment=0) -> sumsd_list

df_list<-t(data.frame(rbind(round(summean_list,2),round(sumsd_list,2))))
df<-cbind(df_imp,df_list)

rownames(df)<-c("Redistribution","Income [1,000 PPP USD]",
                "Income distance","Occupational unemployment [in %]",
                "Age [in years]", "Education [in years]",
                "Benefit concentration","Gini(disposable income)",
                "Immigration","Social spending", "Top tax","Redistribution via transfers",
                "Single","Female","Unemployed")
colnames(df)<-c("Mean","SD","Mean","SD")


##########################
#Produces Table C.4: Descriptive statistics. Means, standard deviations, and percentages.
##########################

xtable(df, caption="Descriptive statistics (comparing multiple 
imputation and listwise deletion). Means, standard deviations 
       and percentages.")

